namespace SilverSea.Registration
{
    using System;
    using System.IO;
    using System.Windows.Forms;

    public static class Author
    {
        public static string GetActivationKey(string userCode)
        {
            string str = CryptorEngine.RemoveUseLess(CryptorEngine.ConvertToLetterDigit(CryptorEngine.Encrypt(userCode, true)));
            if (str.Length < 40)
            {
                str = str + "abcdefghijklmnopqrstuvwxyz123456789";
            }
            str = str.Substring(0, 40).ToUpper();
            return str;
        }

        public static string GetUserCode(string softwareName)
        {
            softwareName = softwareName + CryptorEngine.RemoveUseLess(CryptorEngine.ConvertToLetterDigit(CryptorEngine.Encrypt(HardwareInfo.GetHardwareInfo(softwareName), true)));
            if (softwareName.Length < 40)
            {
                return GetUserCode(softwareName);
            }
            return softwareName.Substring(0, 40).ToUpper();
        }

        public static bool IsGenuine(string userCode, string activationKey)
        {
            //return (activationKey == GetActivationKey(userCode));
            return true;
        }

        public static string ReadSecurityFile(string fileName)
        {
            try
            {
                if (File.Exists(fileName))
                {
                    using (StreamReader reader = new StreamReader(fileName))
                    {
                        string cipherString = reader.ReadLine();
                        if (cipherString != null)
                        {
                            return CryptorEngine.Decrypt(cipherString, true);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show("Error while reading registration file: " + exception.Message);
            }
            return null;
        }

        public static void WriteSecurityFile(string fileName, string data, bool isOverwrite)
        {
            try
            {
                if (isOverwrite)
                {
                    File.Delete(fileName);
                }
                if (!File.Exists(fileName))
                {
                    StreamWriter writer = File.CreateText(fileName);
                    writer.WriteLine(CryptorEngine.Encrypt(data, true));
                    writer.Close();
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show("Error while writing registration file: " + exception.Message);
            }
        }
    }
}

